import argparse, sys, logging, os

from protolib import get_app_root

parser = argparse.ArgumentParser()
parser.add_argument('-l', '--log', help='Set the logging level', default='WARNING')
args = parser.parse_args()

numeric_level = getattr(logging, args.log.upper(), None)
if not isinstance(numeric_level, int):
    raise ValueError('Invalid log level: %s' % args.log)

logging.basicConfig(
    level=numeric_level,
    datefmt='%m/%d/%Y %I:%M:%S',
    format='[%(asctime)s] %(name)s %(levelname)s: %(message)s'
)

logger = logging.getLogger(__name__)

fh = logging.FileHandler(os.path.join(get_app_root(), 'FileBrowser.log'))
fh.setLevel(logging.DEBUG)
fh_formatter = logging.Formatter('[%(asctime)s] %(name)s %(levelname)s: %(message)s')
fh_formatter.datefmt = '%m/%d/%Y %I:%M:%S'
fh.setFormatter(fh_formatter)
logging.getLogger().addHandler(fh)

logger.info('Log file handler initialized')

if __name__ == '__main__':
    import gui
    gui.main(args)
